package cn.edu.zjicm.wordsnet_d.synchronize;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.thrift.TException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.data.DataManager;
import cn.edu.zjicm.wordsnet_d.data.MobclickAgentData;
import cn.edu.zjicm.wordsnet_d.data.SynData;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.download.JudgeNetwork;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;

public class UserInterface extends ServerInterface {

	private static UserInterface mInstance;

	public static UserInterface getInstance(Context context) {
		if (mInstance == null) {
			mInstance = new UserInterface(context);
		}
		return mInstance;
	}

	private UserInterface(Context context) {
		super(context);
	}

	public boolean isLogin() {
		return AppPreference.getLoginID(context) != "" && AppPreference.getLoginID(context) != "guest";
	}

	public void logout() {
		AppPreference.setLoginID(context, "");
		AppPreference.setSyncVC(context, 0);
		AppPreference.setEXP(context, 0);
		AppPreference.setNickName(context, null);
		AppPreference.setSchoolName(context, null);
		AppPreference.setSchoolId(context, -1);
		AppPreference.setHeadPortrait(context, null);
		AppPreference.setAccessTokenWeibo(context, null);
		AppPreference.setWeChatUnionid(context, null);
		AppPreference.setWeChatOpenid(context, null);
		AppPreference.setNoPunchDate(context, "");
		DataManager.getInstance().clearUserData(context);
		Toast.makeText(context, "已注销", Toast.LENGTH_SHORT).show();
		// Log.d("Tag",
		// "退出登录后，保存的bookId是："+AppPreference.getSelectedBookId(context));
		MobclickAgentData.logout(context);
	}

	public void register(String loginID, String password, Handler handler) {
		if (JudgeNetwork.getInstance(context).isMobileConnected() || JudgeNetwork.getInstance(context).isWifiConnected()) {
			new RegisterThread(loginID, password, handler).start();
		} else {
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_BAD_NETWORK));
		}
		MobclickAgentData.register(context, "尝试注册");
	}

	public class RegisterThread extends Thread {
		String loginID;
		String password;
		String token;
		Handler handler;

		RegisterThread(String loginID, String password, Handler handler) {
			this.loginID = loginID;
			this.password = password;
			this.token = "";
			this.handler = handler;
		}

		public void run() {
			DefaultHttpClient client = new DefaultHttpClient();
			setClientTimeout(client);

			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = new BasicNameValuePair("loginId", DefaultTripleDES.getInstance().encrypt(loginID));
			NameValuePair pair2 = new BasicNameValuePair("password", DefaultTripleDES.getInstance().encrypt(password));
			NameValuePair pair3 = new BasicNameValuePair("exp", DefaultTripleDES.getInstance().encrypt(AppPreference.getEXP(context) + ""));
			list.add(pair1);
			list.add(pair2);
			list.add(pair3);
			try {
				HttpEntity entity = new UrlEncodedFormEntity(list);
				HttpPost post = new HttpPost(REGISTER_URL);
				post.setEntity(entity);
				HttpResponse response = client.execute(post);

				if (response.getStatusLine().getStatusCode() == 200) {
					// InputStream in =
					// response.getEntity().getContent();//接收服务器的数据，并在Toast上显示
					// String str = readString(in);
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					str = DefaultTripleDES.getInstance().decrypt(str);
					LogUtil.d("注册,str=" + str);

					JSONObject result = new JSONObject(str);
					String message = result.getString("message");
					boolean success = result.getBoolean("success");
					if (success) {
						token = result.getString("t");
						AppPreference.setLoginID(context, loginID);
						AppPreference.setPassword(context, password);
						AppPreference.setToken(context, token);
						AppPreference.setNickName(context, loginID);
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS));
					} else {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_REGISTERED));
					}
					// String str1 = message + "\n" + success + "\n" + token;
					// Toast.makeText(context, str1, Toast.LENGTH_SHORT).show();
				} else {
					handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ERROR));
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
				if (e instanceof ConnectTimeoutException) {
					MobclickAgentData.register(context, "注册失败，请求超时");
				} else if (e instanceof SocketTimeoutException) {
					MobclickAgentData.register(context, "注册失败，响应超时");
				} else {
					MobclickAgentData.register(context, "注册失败:" + e.getMessage());
				}
			}
		}
	};

	public void login(String loginID, String password, Handler handler) {
		if (JudgeNetwork.getInstance(context).isMobileConnected() || JudgeNetwork.getInstance(context).isWifiConnected()) {
			new LoginThread(loginID, password, handler).start();
		} else {
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_BAD_NETWORK));
		}

		MobclickAgentData.login(context, "尝试登录");
	}

	public class LoginThread extends Thread {
		String loginID;
		String password;
		String token;
		Handler handler;

		LoginThread(String loginID, String password, Handler handler) {
			this.loginID = loginID;
			this.password = password;
			this.token = "";
			this.handler = handler;
		}

		public void run() {

			DefaultHttpClient client = new DefaultHttpClient();
			setClientTimeout(client);

			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = new BasicNameValuePair("loginId", DefaultTripleDES.getInstance().encrypt(loginID));
			NameValuePair pair2 = new BasicNameValuePair("password", DefaultTripleDES.getInstance().encrypt(password));
			list.add(pair1);
			list.add(pair2);
			try {
				HttpEntity entity = new UrlEncodedFormEntity(list);
				HttpPost post = new HttpPost(LOGIN_URL);
				post.setEntity(entity);
				HttpResponse response = client.execute(post);

				if (response.getStatusLine().getStatusCode() == 200) {
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					str = DefaultTripleDES.getInstance().decrypt(str);
					LogUtil.d(str);
					// Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
					JSONObject result = new JSONObject(str);
					String message = result.getString("message");
					boolean success = result.getBoolean("success");
					if (success) {
						AppPreference.setLoginID(context, loginID);
						AppPreference.setPassword(context, password);
						token = result.getString("t");
						AppPreference.setToken(context, token);
						if (result.has("n"))
							AppPreference.setNickName(context, result.getString("n"));
						if (!result.isNull("schoolid"))
							AppPreference.setSchoolId(context, result.getInt("schoolid"));
						if (result.has("school"))
							AppPreference.setSchoolName(context, result.getString("school"));
						if (result.has("exp"))
							AppPreference.setEXP(context, result.getInt("exp"));
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS));
					} else {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_INCORRECT_PASSWORD));
					}
					// String str1 = message + "\n" + success + "\n" + token;
					// Toast.makeText(context, str1, Toast.LENGTH_SHORT).show();
				} else {
					handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ERROR));
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
				if (e instanceof ConnectTimeoutException) {
					MobclickAgentData.login(context, "登录失败，请求超时");
				} else if (e instanceof SocketTimeoutException) {
					MobclickAgentData.login(context, "登录失败，响应超时");
				} else {
					MobclickAgentData.login(context, "登录失败:" + e.getMessage());
				}
			}
		}
	};

	public void loginWeixin(String unionId, String pic, String nickName, Handler handler) {
		if (JudgeNetwork.getInstance(context).isWifiConnected() || JudgeNetwork.getInstance(context).isMobileConnected()) {
			new LoginWeixinThread(unionId, pic, nickName, handler).start();
		} else {
			// Toast.makeText(context, "请确认网络是否连接", Toast.LENGTH_LONG).show();
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_BAD_NETWORK));
		}
		MobclickAgentData.loginWeixin(context, "尝试微信登录");
	}

	public class LoginWeixinThread extends Thread {
		String unionId;
		String pic;
		String nickName;
		String token;
		Handler handler;

		LoginWeixinThread(String unionId, String pic, String nickName, Handler handler) {
			this.unionId = unionId;
			this.pic = pic;
			this.nickName = nickName;
			this.token = "";
			this.handler = handler;
		}

		public void run() {
			DefaultHttpClient client = new DefaultHttpClient();
			setClientTimeout(client);

			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = new BasicNameValuePair("uid", unionId);
			NameValuePair pair2 = new BasicNameValuePair("p", pic);
			NameValuePair pair3 = new BasicNameValuePair("n", nickName);
			NameValuePair pair4 = new BasicNameValuePair("exp", AppPreference.getEXP(context) + "");
			list.add(pair1);
			list.add(pair2);
			list.add(pair3);
			list.add(pair4);
			try {
				HttpEntity entity = new UrlEncodedFormEntity(list, "utf-8");
				HttpPost post = new HttpPost(LOGIN_Weixin_URL);
				post.setEntity(entity);
				post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");
				HttpResponse response = client.execute(post);

				if (response.getStatusLine().getStatusCode() == 200) {
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					// str = DefaultTripleDES.getInstance().decrypt(str);
					LogUtil.addLog("unionId：" + unionId);
					LogUtil.addLog("pic：" + pic);
					LogUtil.addLog("昵称：" + nickName);
					LogUtil.addLog("微信登录：" + str);
					JSONObject result = new JSONObject(str);
					String message = result.getString("message");
					boolean success = result.getBoolean("success");
					if (success) {
						// AppPreference.setLoginID(context, unionId);
						// AppPreference.setPassword(context, password);
						token = result.getString("t");
						AppPreference.setToken(context, token);
						if (result.has("n"))
							AppPreference.setNickName(context, result.getString("n"));
						if (!result.isNull("schoolid"))
							AppPreference.setSchoolId(context, result.getInt("schoolid"));
						if (result.has("school"))
							AppPreference.setSchoolName(context, result.getString("school"));
						if (result.has("exp"))
							AppPreference.setEXP(context, result.getInt("exp"));
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS, result.has("exp"))); // result.has("exp")
																													// fale表示第一次微信登录，ture表示之前已用微信账号登陆过
					} else {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_INCORRECT_PASSWORD));
					}
					// String str1 = message + "\n" + success + "\n" + token;
					// Toast.makeText(context, str1, Toast.LENGTH_SHORT).show();
				} else {
					handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ERROR));
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
				if (e instanceof ConnectTimeoutException) {
					MobclickAgentData.loginWeixin(context, "微信登录失败，请求超时");
				} else if (e instanceof SocketTimeoutException) {
					MobclickAgentData.loginWeixin(context, "微信登录失败，响应超时");
				} else {
					MobclickAgentData.loginWeixin(context, "微信登录失败:" + e.getMessage());
				}
			}
		}
	};

	public void sync(Handler handler) {
		// 如果需要同步的单词数为0，不同步，直接返回成功
		// 如果isFirstTimeStudyNewBook为true，表示修改过学习计划，也需要同步
		if (AppPreference.getSyncVC(context) > 0 && WordFactory.getInstance(context).getWordsLogCount() == 0 && !AppPreference.isFirstTimeStudyNewBook(context)) {
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS));
		} else if (JudgeNetwork.getInstance(context).isWifiConnected() || JudgeNetwork.getInstance(context).isMobileConnected()) {
			new InnerSyncThreadOfThrift(handler).start();
		} else {
			// Toast.makeText(context, "请确认网络是否连接", Toast.LENGTH_LONG).show();
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_BAD_NETWORK));
			MobclickAgentData.sync(context, "同步失败，未检测到网络连接");
		}
		MobclickAgentData.sync(context, "尝试同步");
	}

	public class InnerSyncThreadOfThrift extends Thread {
		String token;
		String data;
		String vc; //
		String wc; // 同步的单词个数
		String cv = "101"; // v2.11.0将cv从1修改成101
		Handler handler;

		InnerSyncThreadOfThrift(Handler handler) {
			this.token = AppPreference.getToken(context);
			this.vc = AppPreference.getSyncVC(context) + "";
			SynData synData = SynData.createSynData(context);
			byte b[] = null;
			try {
				b = ThriftSerialization.serialize(synData);
				this.data = StringZip.compressFromByteArray(b);
				// this.data = new String(b, "US-ASCII");// "测试郑�B";
			} catch (IOException e) {
				e.printStackTrace();
			} catch (TException e) {
				e.printStackTrace();
			}
			this.wc = synData.getUserData().size() + "";
			// if(synData.getUserData().size() == 0)
			// this.wc = "-1";
			// Log.e("vc", this.vc);
			// Log.e("jasondata", this.data);
			this.handler = handler;
		}

		public void run() {
			DefaultHttpClient client = new DefaultHttpClient();
			setClientTimeout(client);

			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = new BasicNameValuePair("t", token);
			NameValuePair pair2 = new BasicNameValuePair("data", data);
			NameValuePair pair3 = new BasicNameValuePair("vc", vc);
			NameValuePair pair4 = new BasicNameValuePair("wc", wc);
			NameValuePair pair5 = new BasicNameValuePair("cv", cv);
			NameValuePair pair6 = new BasicNameValuePair("transfer_codec", "thrift"); // "transfer_codec",默认为"hessian"
			NameValuePair pair7 = new BasicNameValuePair("exp", AppPreference.getEXP(context) + ""); // "transfer_codec",默认为"hessian"
			list.add(pair1);
			list.add(pair2);
			list.add(pair3);
			list.add(pair4);
			list.add(pair5);
			list.add(pair6);
			list.add(pair7);
			try {
				HttpEntity entity = new UrlEncodedFormEntity(list);
				HttpPost post = new HttpPost(INNER_SYNC_URL);
				post.setEntity(entity);
				HttpResponse response = client.execute(post);

				LogUtil.i("同步时,vc=" + vc + ",wc=" + wc);

				if (response.getStatusLine().getStatusCode() == 200) {
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					JSONObject result = new JSONObject(str);
					String message = result.getString("message");
					boolean success = result.getBoolean("success");

					LogUtil.d("result:" + result);

					if (success) {
						long time = result.getLong("time");
						int vc = result.getInt("vc");
						boolean isUpload = result.getBoolean("isUpload");
						String data = "";
						if (isUpload) {
							AppPreference.setSyncTime(context, time);
							AppPreference.setSyncVC(context, vc);
							MobclickAgentData.sync(context, "upload");
						} else {
							AppPreference.setSyncTime(context, time);
							AppPreference.setSyncVC(context, vc);
							data = result.getString("data");
							byte b[] = StringZip.unCompressToByteArray(data);
							SynData userSynData = ThriftSerialization.deserialize(b);
							// SynData userSynData =
							// (SynData)deserialize(data.getBytes("US-ASCII"));
							SynData.resetSynData(userSynData, context);
							// Log.e("reset jasondata", data);
							MobclickAgentData.sync(context, "download");
						}
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS, data));
					} else {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_UNSUCCESS));
						MobclickAgentData.sync(context, "同步失败");
					}
					// String str1 = message + "\n" + success + "\n" + token;
					// Toast.makeText(context, str1, Toast.LENGTH_SHORT).show();
				} else {
					handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
					MobclickAgentData.sync(context, "同步失败，服务器错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
				MobclickAgentData.sync(context, "同步失败，连接服务器失败");
				if (e instanceof ConnectTimeoutException) {
					MobclickAgentData.sync(context, "同步失败，请求超时");
				} else if (e instanceof SocketTimeoutException) {
					MobclickAgentData.sync(context, "同步失败，响应超时");
				} else {
					MobclickAgentData.sync(context, "同步失败:" + e.getMessage());
				}
			}
		}

	};

	public void modifyPassword(String oldPassword, String password, Handler handler) {
		if (JudgeNetwork.getInstance(context).isWifiConnected() || JudgeNetwork.getInstance(context).isMobileConnected()) {
			new ModifyPasswordThread(oldPassword, password, handler).start();
		} else {
			// Toast.makeText(context, "请确认网络是否连接", Toast.LENGTH_LONG).show();
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_BAD_NETWORK));
		}
		MobclickAgentData.modifyPassword(context);
	}

	public class ModifyPasswordThread extends Thread {
		String loginId;
		String oldPassword;
		String password;
		Handler handler;

		ModifyPasswordThread(String oldPassword, String password, Handler handler) {
			this.oldPassword = oldPassword;
			this.password = password;
			this.loginId = AppPreference.getLoginID(context);
			this.handler = handler;
		}

		public void run() {
			DefaultHttpClient client = new DefaultHttpClient();
			setClientTimeout(client);

			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = new BasicNameValuePair("loginId", DefaultTripleDES.getInstance().encrypt(loginId));
			NameValuePair pair2 = new BasicNameValuePair("oldPwd", DefaultTripleDES.getInstance().encrypt(oldPassword));
			NameValuePair pair3 = new BasicNameValuePair("newPwd", DefaultTripleDES.getInstance().encrypt(password));
			list.add(pair1);
			list.add(pair2);
			list.add(pair3);
			try {
				HttpEntity entity = new UrlEncodedFormEntity(list);
				HttpPost post = new HttpPost(MODIFY_PASSWORD_URL);
				post.setEntity(entity);
				HttpResponse response = client.execute(post);

				if (response.getStatusLine().getStatusCode() == 200) {
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					str = DefaultTripleDES.getInstance().decrypt(str);
					JSONObject result = new JSONObject(str);
					String message = result.getString("message");
					boolean success = result.getBoolean("success");
					if (success) {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS));
					} else {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WRONG_PASSWORD));
					}
				} else {
					handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
			}
		}
	};

	public void changeNickName(String nick, Handler handler) {
		if (JudgeNetwork.getInstance(context).isWifiConnected() || JudgeNetwork.getInstance(context).isMobileConnected()) {
			new InnerChangeNickNameThread(nick, handler).start();
		} else {
			// Toast.makeText(context, "请确认网络是否连接", Toast.LENGTH_LONG).show();
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_BAD_NETWORK));
		}
		MobclickAgentData.changeNickName(context);
	}

	public class InnerChangeNickNameThread extends Thread {
		String token;
		String nickname;
		Handler handler;

		InnerChangeNickNameThread(String nickname, Handler handler) {
			this.nickname = nickname;
			this.token = AppPreference.getToken(context);
			this.handler = handler;
		}

		public void run() {
			DefaultHttpClient client = new DefaultHttpClient();
			setClientTimeout(client);

			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = new BasicNameValuePair("nick", nickname);
			NameValuePair pair2 = new BasicNameValuePair("t", token);
			list.add(pair1);
			list.add(pair2);
			try {
				HttpEntity entity = new UrlEncodedFormEntity(list, "utf-8");
				HttpPost post = new HttpPost(INNER_CHANGE_NICKNAME);
				post.setEntity(entity);
				post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");
				HttpResponse response = client.execute(post);

				if (response.getStatusLine().getStatusCode() == 200) {
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					JSONObject result = new JSONObject(str);
					String message = result.getString("message");
					boolean success = result.getBoolean("success");
					if (success) {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS));
					} else {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_UNSUCCESS));
					}
				} else {
					handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
			}
		}
	};

	public void setSchoolID(int sid, Handler handler) {
		if (JudgeNetwork.getInstance(context).isWifiConnected() || JudgeNetwork.getInstance(context).isMobileConnected()) {
			new SetSchoolIDThread(sid, handler).start();
		} else {
			// Toast.makeText(context, "请确认网络是否连接", Toast.LENGTH_LONG).show();
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_BAD_NETWORK));
		}
		MobclickAgentData.changeNickName(context);
	}

	public class SetSchoolIDThread extends Thread {
		String token;
		int sid;
		Handler handler;

		SetSchoolIDThread(int sid, Handler handler) {
			this.sid = sid;
			this.token = AppPreference.getToken(context);
			this.handler = handler;
		}

		public void run() {
			DefaultHttpClient client = new DefaultHttpClient();
			setClientTimeout(client);

			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = new BasicNameValuePair("sid", sid + "");
			NameValuePair pair2 = new BasicNameValuePair("t", token);
			list.add(pair1);
			list.add(pair2);
			try {
				HttpEntity entity = new UrlEncodedFormEntity(list, "utf-8");
				HttpPost post = new HttpPost(SET_SCHOOL_ID);
				post.setEntity(entity);
				HttpResponse response = client.execute(post);

				if (response.getStatusLine().getStatusCode() == 200) {
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					JSONObject result = new JSONObject(str);
					String message = result.getString("message");
					boolean success = result.getBoolean("success");
					if (success) {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS));
					} else {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_UNSUCCESS));
					}
				} else {
					handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
			}
		}
	};
}
